package com.le.basic.tree;

/**
 * [题目] 在二叉树中找到一个节点的后继节点
 * 该结构比普通二叉树节点结构多了一个指向父节点的parent指针。
 * 假 设有一 棵Node类型的节点组成的二叉树，树中每个节点的parent指针 都正确地指向 自己的父节点，
 * 头节点的parent指向null。只给一个在 二叉树中的某个节点 node，请实现返回node的后继节点的函数。
 * 在二 叉树的中序遍历的序列中， node的下一个节点叫作node的后继节点
 */
public class Code_03_SuccessorNode {

    public static class Node {
        public int value;
        public Node left;
        public Node right;
        public Node parent;

        public Node(int data) {
            this.value = data;
        }
    }

    /**
     * 返回当前结点的后继结点
     * 1. 当前结点存在右子树,则是右子树最左边的结点
     * 2. 当前结点不存在右子树, 则向上寻找,直到当前结点是父节点的左子树,或者父节点为null
     * @param node
     * @return
     */
    public static Node getSuccessorNode(Node node){
        if (node == null){
            return null;
        }
        if (node.right != null){
            return getLeftMost(node.right);
        }else {
            Node parent = node.parent;
            while (parent != null && parent.left != node){
                node = parent;
                parent = node.parent;
            }
            return parent;
        }
    }

    /**
     * 选择右子树最左结点
     * @param node
     * @return
     */
    public static Node getLeftMost(Node node){
        while (node.left != null){
            node = node.left;
        }
        return node;
    }

    /**
     * 返回当前结点前驱结点
     * 1. 当前结点存在左子树,则是左子树最右结点
     * 2. 不存在左子树, 当前结点是否是父节点右子树
     * @param node
     * @return
     */
    public static Node getPreNode(Node node){
        if (node == null){
            return null;
        }
        if (node.left != null){
            return getLeftMostRight(node.left);
        }else {
            Node parent = node.parent;
            if (parent != null && parent.right != node){
                node = parent;
                parent = node.parent;
            }
            return parent;
        }
    }

    public static Node getLeftMostRight(Node node){
        while (node.right != null){
            node = node.right;
        }
        return node;
    }

    public static void main(String[] args) {
        Node head = new Node(6);
        head.parent = null;
        head.left = new Node(3);
        head.left.parent = head;
        head.left.left = new Node(1);
        head.left.left.parent = head.left;
        head.left.left.right = new Node(2);
        head.left.left.right.parent = head.left.left;
        head.left.right = new Node(4);
        head.left.right.parent = head.left;
        head.left.right.right = new Node(5);
        head.left.right.right.parent = head.left.right;
        head.right = new Node(9);
        head.right.parent = head;
        head.right.left = new Node(8);
        head.right.left.parent = head.right;
        head.right.left.left = new Node(7);
        head.right.left.left.parent = head.right.left;
        head.right.right = new Node(10);
        head.right.right.parent = head.right;

        Node test = head.left;
//        System.out.println(test.value + " next: " + getSuccessorNode(test).value);
        System.out.println(getPreNode(test).value);
    }


}
